using System;
using System.Xml;
using System.Collections.ObjectModel;
using HMCollada.Core;

namespace HMCollada.Core {
    internal struct HMColladaAsset {
        internal Collection<HMColladaContributor> contributor;
        internal DateTime created;
        internal string keywords;
        internal DateTime modified;
        internal string revision;
        internal string subject;
        internal string title;
        internal float unit_meter;
        internal string unit_name;
        internal HMColladaUpAxis up_axis;
    }

    // Ordering is important here, the COLLADA standard says Y_UP is default
    internal enum HMColladaUpAxis {
        Y_UP,
        Z_UP,
        X_UP,
    }
}

namespace HMCollada {
    internal partial class HMColladaLoader {
        internal static HMColladaAsset LoadAsset() {
            HMColladaAsset newAsset = new HMColladaAsset();

            newAsset.contributor = new Collection<HMColladaContributor>();

            if (!colladaReader.IsEmptyElement) colladaReader.Read();
            do {
                if (colladaReader.NodeType != XmlNodeType.EndElement) {
                    switch (colladaReader.Name) {
                        case "contributor": newAsset.contributor.Add(LoadContributor()); break;
                        case "created": newAsset.created = DateTime.Parse(ReadString().Trim()); break;
                        case "keywords": newAsset.keywords = ReadString().Trim(); break;
                        case "modified": newAsset.modified = DateTime.Parse(ReadString().Trim()); break;
                        case "revision": newAsset.revision = ReadString().Trim(); break;
                        case "subject": newAsset.subject = ReadString().Trim(); break;
                        case "title": newAsset.title = ReadString().Trim(); break;
                        case "unit":
                            newAsset.unit_meter = float.Parse(colladaReader.GetAttribute("meter"));
                            newAsset.unit_name = colladaReader.GetAttribute("name");
                            colladaReader.Read();
                            break;

                        case "up_axis":
                            string axisString = ReadString().Trim();
                            newAsset.up_axis =
                                axisString == "X_UP" ? HMColladaUpAxis.X_UP :
                               (axisString == "Z_UP" ? HMColladaUpAxis.Z_UP : HMColladaUpAxis.Y_UP);
                            break;
                    }
                }

                if (!colladaReader.IsEmptyElement) colladaReader.Read();
            } while (colladaReader.Name != "asset");

            if (colladaReader.IsEmptyElement) colladaReader.Read();
            return newAsset;
        }
    }
}